package com.yuejie.user.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yuejie.db.DBConnection;
import com.yuejie.user.manager.UserInfoManager;
import com.yuejie.util.MD5Util;

/**
 * @ClassName: UserInfoManagerImpl
 * @Description: 用户管理
 * @author husongbo
 * @date 2012-7-3 下午02:21:13
 * 
 */

public class UserInfoManagerImpl implements UserInfoManager{
	
	DBConnection db = null;
	
	public UserInfoManagerImpl()
	{
		db = new DBConnection();
	}
	
	public String userRegister(String psn_account, String psn_password) throws Exception
	{
		String result = "failed";
		String password = MD5Util.MD5(psn_password);
		Object[] params = new Object[]{psn_account, password};;
		List<Object[]> paramList = new ArrayList<Object[]>();
		
		paramList.add(params);
		
		boolean flag = true;
		if(judgePerson(psn_account))
		{
			 flag = db.insertSMSInfo(INSERT_SQL, paramList); 
			 if(flag)
			 {
				 result = "success";
			 }
		}
		else
		{
			result = "duplicated";
		}

		db.close();
		
		return result;
	}
	
	public boolean judgePerson(String psn_account) throws Exception
	{
		Object[] params = new Object[]{psn_account};
		List<Map<String, Object>> list = db.querySMSInfo(SELECT_SQL, params);
		
		if(list != null && list.size() != 0) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	private final static String INSERT_SQL = "INSERT INTO PERSON(PSN_ACCOUNT, PSN_PASSWORD) VALUES(?, ?)";
	
	private final static String SELECT_SQL = "SELECT PSN_ACCOUNT, PSN_PASSWORD FROM PERSON WHERE PSN_ACCOUNT = ?";

}
